package com.ktjiaoyu.controller;

import com.ktjiaoyu.pojo.News;
import com.ktjiaoyu.service.NewsService;
import com.ktjiaoyu.utils.R;
import com.ktjiaoyu.utils.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * projectName: com.atguigu.controller
 * <p>
 * <p>
 * description:
 */

@CrossOrigin
@RestController
@RequestMapping("api/admin")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @PostMapping("news/page")
    public R findNews(String title, String author, Integer typeId, Integer pageNum, Integer pageSize) {
        pageNum = RequestUtil.parseInt(pageNum, 1);
        pageSize = RequestUtil.parseInt(pageSize, 5);
        R r = newsService.SelectNewsByPage(title, author, typeId, pageNum, pageSize);
        return r;
    }

    @DeleteMapping("{id}")
    public R deleteNews(@PathVariable Integer id) {
        R r = newsService.deleteNewsById(id);
        return r;
    }

    @PostMapping("newsBatch")
    public R deleteNewsByIds(@RequestBody List<Integer> multipleSelection) {
        R r = newsService.deleteNewsByIds(multipleSelection);
        return r;
    }


    @PostMapping("newsType")
    public R deleteNewsByIds(@RequestBody News news) {
        R r = newsService.saveNews(news);
        return r;
    }
}
